package com.sun.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.web.domain.ResultMsg;
import com.sun.web.domain.User;
import com.sun.web.framework.TokenHolder;
import com.sun.web.service.UserService;
import com.sun.web.test.UserTest;
import com.sun.web.utils.ApplicationContext;
import com.sun.web.utils.CipherUtils;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@Autowired
	private ResultMsg resultMsg;
	
	@RequestMapping(value = "/register.do")		//可以通过在这里加入produces = "application/json; charset=utf-8"，保证返回结果不会乱码
	@ResponseBody
	public ResultMsg register(
			@RequestParam("username")String username,		//客户端传的参数必须要和@RequestParam中的参数名称相同
			@RequestParam("password")String password) throws Exception{
		
		//由于默认的tomcat配置，接收请求是以ISO-8859-1来转码，导致中文出现了乱码问题，只要能正确的以utf-8来转码，则可以解决乱码问题。
		//username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
		
		/*System.out.println("recv username:" + username);
		System.out.println("recv password:" + password);
		
		username = CipherUtils.decryptAES(username, CipherUtils.string2key(ApplicationContext.getSecretKeyString()));
		password = CipherUtils.decryptAES(password, CipherUtils.string2key(ApplicationContext.getSecretKeyString()));
		
		System.out.println("decrypt usernamme = " + username);
		System.out.println("decrypt passwword = " + password);*/
		
		ResultMsg resultMsg = new ResultMsg();
		
		if (userService.checkUserExist(username, password)){
			resultMsg.setresultCode(100);
			resultMsg.setresultMsg("用户已经存在");
			return resultMsg;
		}
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		userService.insertUser(user);
		resultMsg.setData(user.getId());
		
		return resultMsg;
	}
	
	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	@ResponseBody
	public ResultMsg login(
			@RequestParam("username")String username,
			@RequestParam("password")String password){
		
		if (userService.checkUserExist(username, password)){
			
			String token = CipherUtils.EncryptWithSha256(username + password);
			User user = userService.getUserByUsername(username);
			
//			System.out.println("user:" + user.getId() + user.getUsername());
			user.setToken(token);
			userService.updateUser(user);
			
			resultMsg.setresultCode(200);
			resultMsg.setresultMsg(token);
		}else{
			resultMsg.setresultCode(100);
			resultMsg.setresultMsg("用户不存在");
		}
		
		return resultMsg;
	}
	
	
	@RequestMapping(value = "/update.do", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg updateUser(
			@RequestParam("id")int userid,
			@RequestParam("avatar")String avatar,
			@RequestParam("gender")int gender,
			@RequestParam("email")String email,
			@RequestParam("phone")String phone){
		
		System.out.println(avatar);
		
		ResultMsg resultMsg = new ResultMsg();
		User user = userService.getUserById(userid);
		
		user.setAvatar(avatar);
		user.setGender(gender);
		user.setEmail(email);
		user.setPhone(phone);
		
		userService.updateUser(user);
		
		return resultMsg;
	}
	

	@RequestMapping(value = "/view.do", method = RequestMethod.GET)
	@ResponseBody
	public ResultMsg getUserInfo(@RequestParam("userid")int userid, 
			@RequestParam("token")String token){			//url传的参数名称必须是：userid
		
		try {
			
			User user = TokenHolder.getUser();
			
//			User user = userService.getUserById(userid);
			resultMsg.setresultCode(200);
			resultMsg.setresultMsg("success");
			resultMsg.setData(user);
		} catch (Exception e) {
			// TODO: handle exception
			resultMsg.setresultCode(100);
			resultMsg.setresultMsg("error");
			resultMsg.setData(null);
			return resultMsg;
		}
		
		
		return resultMsg;
	}
	

	
	@RequestMapping(value = "/list.do", method = RequestMethod.GET)
	@ResponseBody
	public ResultMsg getUserList(){			//url传的参数名称必须是：userid
		
		try {
			System.out.println("getUserList before....");
			List<User> users = userService.listUsers();
			System.out.println("getUserList end....");
			resultMsg.setresultCode(200);
			resultMsg.setresultMsg("success");
			resultMsg.setData(users);
		} catch (Exception e) {
			// TODO: handle exception
			resultMsg.setresultCode(100);
			resultMsg.setresultMsg("error");
			resultMsg.setData(null);
			return resultMsg;
		}
		
		
		return resultMsg;
	}
	
	@RequestMapping(value = "/count.do", method = RequestMethod.GET)
	@ResponseBody
	public ResultMsg getUserCount(){
		
		System.out.println("controller getUserCount...");
		
		int count = userService.getUserCount();
		
		resultMsg.setData(count);
		
		return resultMsg;
	}
	
}
